package com.giahoang.web;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(LoginController.class);

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(
            @RequestParam(value = "login_error", required = false) String loginError,
            ModelMap model, HttpServletRequest request,
            Authentication authentication) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Login attemp from " + request.getRemoteAddr());
        }
        if(authentication==null)
        {
            return "login";
        }
        else
        {
            return "redirect:/";
        }
        
    }

    @RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
    public String loginFail(ModelMap model) {
        return "login";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(ModelMap model) {
        return "logout";
    }

}